﻿var address=new Array();

for(var k = 0;k < 30;k++)
    {
        address[k] = new Array();
    }
function Mask()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "block"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "block";     
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "block";
    cart.style.position = "absolute";
    var bigImg = document.getElementById("SendMethod");
    var pleft = 0;
    var ptop = 0;
   
    var obj = bigImg.offsetParent;
 
    while(obj.tagName != "BODY")
    {
         pleft = pleft + obj.offsetLeft;
         ptop = ptop + obj.offsetTop;
         obj = obj.offsetParent;
    }
    ptop = ptop +50;
    cart.style.left = pleft +200+ "px";
    cart.style.top = ptop +"px";
    cart.style.width = 300 + "px";
    cart.style.backgroundColor  = "#ffffff";
}

function HidMask()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "none"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "none";   
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "none";    
}
function SetAddress(data)
{    
    document.getElementById("ShoppingCart").innerHTML=data;
}
function GetWLList()
{
    document.getElementById("div_loadding").innerHTML = "<span ><img src='../Include/Images/loadimg.gif'></span>";
     $j.post("../Handler/AddressHandler.ashx", {OP:"WL"},
            function(data){                          
            document.getElementById("div_wl").innerHTML=data;
           document.getElementById("div_loadding").innerHTML ="";
            }
        );
}
function GetddlyList()
{
    document.getElementById("div_loadding").innerHTML = "<span ><img src='../Include/Images/loadimg.gif'></span>";
     $j.post("../Handler/AddressHandler.ashx", {OP:"ddly"},
            function(data){                          
            document.getElementById("div_ddly").innerHTML=data;
           document.getElementById("div_loadding").innerHTML ="";
            }
        );
}
function AddressInit(UserCode)
{
    Mask();
    //SetAddress("<br /><br />数据加载中......<br /><br /><img src='../Include/Images/loadimg.gif'>");  
     $j.post("../Handler/AddressHandler.ashx", {UserCode:UserCode, OP:"Init"},
            function(data){ 
            if(data!="") {
                    for(var k = 0;k < 30;k++)
                        {
                            address[k] = new Array();
                        }
                   eval(data);
                   InitProvince();
                   InitAddressList();
                   HidMask();
               }
            }
        );
}
function InitProvince()
{
    var options = address[0][0];
    var arr_option = options.split("&&");
    var sel = document.getElementById("Province");
    var value=sel.value;
    while( sel.length>0 )
    {
        sel.removeChild(sel.options(0));        
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(value!=null)
    {        
        sel.value=value;
    }
    else
    {
        sel.value="-1";
        var province = document.getElementById("Province"); 
        while( province.length>0 )
        {
            province.removeChild(province.options(0));
        }
        province.options[0]=new Option("请选择","-1");        
    }
}
function GetCityList(province,value)
{//debugger;
    LoadCity=false;
    if(parseInt(province)>-1)
    {
        $j.post("../Handler/AddressHandler.ashx", {Province:province, OP:"City"},
                function(data){                                            
                   InitCity(data,value);                   
                }
            );
     }
     ClearWLGS();
}
function InitCity(data,value)
{
    var arr_option = data.split("&&");
    var sel = document.getElementById("City");  
    
    while(sel.length>0 )
    {
        sel.removeChild(sel.options(0));
       
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(value!=null)
    {        
        sel.value=value;
    }
    else
    {
        sel.value="-1";
        var areas = document.getElementById("Area"); 
        while( areas.length>0 )
        {
            areas.removeChild(areas.options(0));
        }
        areas.options[0]=new Option("请选择","-1");        
    }
    
}
function GetAreaList(value,selectvalue)
{//debugger;
    LoadArea=false;
    if(parseInt(value)>-1)
    {
        $j.post("../Handler/AddressHandler.ashx", {City:value, OP:"Area"},
                function(data){                          
                   InitArea(data,selectvalue);
                }
            );
     }
     else
     {
        var sel = document.getElementById("Area"); 
    
        while( sel.length>0 )
        {
            sel.removeChild(sel.options(0));
            
        }
        sel.options[0]=new Option("请选择","-1");
     }
     ClearWLGS();
}
function ClearWLGS()
{//debugger;
    var deliver=document.getElementById("Deliver");
    if(elems!=null)
    {
        var elems=deliver.getElementsByTagName("input");
        for(var i=0;i<elems.length;i++)
        {
            if(elems[i].type=="radio")
            {
                elems[i].checked=false;
            }
        }
    }
}

function InitArea(data,selectvalue)
{
    var arr_option = data.split("&&");
    var sel = document.getElementById("Area");  
    
    while( sel.length>0 )
    {
        sel.removeChild(sel.options(0));
        
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(selectvalue!=null)
    {
        sel.value=selectvalue;
    }
    else
    {
        sel.value="-1";
    }
}
function InitAddressList()
{
    var up=false;
    var Tablehtml="<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#999999\">"
                               + "<tr style=\"background:#e7e7e7\">"
                               + "<td style=\"font-weight:bold;text-align:center;line-height:22px;\"> "
                               +"&nbsp;</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"收货人姓名</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"具体地址</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"邮编</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"固定电话</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"手机</td>"
                               +"<td style=\"font-weight:bold;text-align:center\">"
                               +"选择地址</td>"
                               +" </tr>";
    for(var i=1;i<30;i++)
    {
        if(address[i][0]!=null)
        {
            Tablehtml=Tablehtml+"<tr style=\"background:#ffffff\">"
                                +"<td style=\"text-align:center;line-height:22px;\">地址"+i.toString()+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][2]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][4]+address[i][6]+address[i][8]+address[i][9]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][10]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][11]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\">"+address[i][12]+"</td>"
                                +"<td style=\"text-align:center;line-height:22px;\"><a href=\"Javascript:\" onclick=\"ShowlatestAddress("+address[i][0]+")\">选择</a></td>"
                                +"</tr>";
            up=true;
        }
    }
    if(up)
    {
        //ShowlatestAddress(address[1][0]);
        document.getElementById("AddressList").innerHTML=Tablehtml;
    }
    else
    {
            document.getElementById("T_Name").value="";
            document.getElementById("Address").value="";
            document.getElementById("Post").value="";
            //document.getElementById("Phone").value="";
            document.getElementById("Mobile").value="";
            document.getElementById("Area").value="-1";
            document.getElementById("City").value="-1";
    }
}
function ShowlatestAddress(ID)
{
    var Province=document.getElementById("Province");
    var City=document.getElementById("City");
    var Area=document.getElementById("Area");   
    for(var x=1;x<30;x++)
    {
        if(address[x][0]==ID)
        {
            document.getElementById("T_Name").value=address[x][2];
            document.getElementById("Address").value=address[x][9];
            document.getElementById("Post").value=address[x][10];
            //document.getElementById("Phone").value=address[x][11];
               var phonearr=address[x][11];
               phonearr=phonearr.split('-');
               
               if(phonearr.length==2)
               {
                    document.getElementById("Phone").value=phonearr[0];  
                    document.getElementById("Phone2").value=phonearr[1];   
                    document.getElementById("Phone3").value="分机";                
               }
               if(phonearr.length==3)
               {  
                    document.getElementById("Phone").value=phonearr[0];  
                    document.getElementById("Phone2").value=phonearr[1]; 
                    document.getElementById("Phone3").value=phonearr[2];                  
               }
            document.getElementById("Mobile").value=address[x][12]; 
            Province.value= address[x][3];
            GetCityList(address[x][3],address[x][5]);
            GetAreaList(address[x][5],address[x][7]);                   
        }
    }
    
}
function Clear(obj)
{
    if(obj.value=="区号"||obj.value=="号码"||obj.value=="分机")
    {
        obj.value="";
    }
}

function CheckAddress()
{//debugger;
    var reciver=document.getElementById("T_Name");
    if(reciver.value.length<1)
    {
        alert("收货人姓名不能为空");
        reciver.focus();
        return false;
    }
    var Province=document.getElementById("Province");
    if(Province.selectedIndex<1)
    {
        alert("请选择省份");
        Province.focus();
        return false;
    }
    var City=document.getElementById("City");
    if(City.selectedIndex<1)
    {
        alert("请选择城市");
        City.focus();
        return false;
    }
    var Area=document.getElementById("Area");
    if(Area.selectedIndex<1)
    {
        alert("请选择区县");
        Area.focus();
        return false;
    }
    var daddress=document.getElementById("Address");
    if(daddress.value.length<1)
    {
        alert("请填写具体地址");
        daddress.focus();
        return false;
    }
    var post=document.getElementById("Post");
    if(isNaN(post.value))
    {
        alert("邮编格式错误,请检查您输入法的全角/半角状态！");
        post.focus();
        return false;
    }
    var regular=/^\d{6}$/;
    if(!regular.test(post.value))
    {
        alert("邮编不正确");
        post.focus();
        return false;
    }
    var phone=document.getElementById("Phone");//区号
    var phone1=document.getElementById("Phone2");//电话号码
    var phone2=document.getElementById("Phone3");//分机号码
    var mobile=document.getElementById("Mobile");
    if((phone1.value.length==0||phone1.value=="号码")&&mobile.value.length==0)
    {alert("手机或固定电话必填一个"); return false;}
    
    if(phone.value.length>0&&phone.value!="区号"||phone1.value.length>0&&phone1.value!="号码"||phone2.value.length>0&&phone2.value!="分机")
        {
        if((phone.value.length>0&&phone.value!="区号")||(phone1.value.length>0&&phone1.value!="号码"))
        {
            if(isNaN(phone.value)||isNaN(phone1.value))
            {
                alert("固定电话格式错误,请检查您输入法的全角/半角状态！");
                return false;
            }
            var reg=/^\d{3,4}$/;//检查区号
            var reg1=/^\d{7,8}$/;//检查电话号码
            if(!reg.test(phone.value)||!reg1.test(phone1.value))
            {
                alert("固定电话格式错误");
                return false;
            }
        }
        if(phone2.value.length>0&&phone2.value!="分机")
        {
            if(isNaN(phone.value)||isNaN(phone1.value)||isNaN(phone2.value))
            {
                alert("固定电话格式错误,请检查您输入法的全角/半角状态！");
                return false;
            }
            var reg=/^\d{3,4}$/;//检查区号
            var reg1=/^\d{7,8}$/;//检查电话号码
            var reg2=/^\d{1,5}$/;//检查分机号
            if(!reg.test(phone.value)||!reg1.test(phone1.value)||!reg2.test(phone2.value))
            {
                alert("固定电话格式错误");
                return false;
            }
        }
    }
    var mobile=document.getElementById("Mobile");
    if(mobile.value.length>0)
    {
        if(isNaN(mobile.value))
        {
            alert("手机号码含有非法字符");
            mobile.focus();
            return false;
        }
        var reg=/^\d{11}$/;//检查手机
        if(!reg.test(mobile.value))
        {
            alert("请输入正确的11位数手机号码");
            mobile.focus();
            return false;
        }
        /*
        var myreg = /^(((13[0-9]{1})|159|153)+\d{8})$/;
        var flag=myreg.test(mobile.value);
        if(!flag)
        {
            alert("手机号码格式错误");
            mobile.focus();
            return false;
        }
        */
    }
    return true;
}
function AddNewAddress()
{    
    if(CheckAddress())
    {
        var reciver=document.getElementById("T_Name").value;
        var Province=document.getElementById("Province").value;
        var City=document.getElementById("City").value;
        var Area=document.getElementById("Area").value;
        var daddress=document.getElementById("Address").value;
        var post=document.getElementById("Post").value;
        
        var UserCode=document.getElementById("TextBox2").value;
        
        var phone0=document.getElementById("Phone");//区号
        var phone1=document.getElementById("Phone2");//电话号码
        var phone2=document.getElementById("Phone3");//分机号码
    
        var phone="";
        if(phone1.value.length>0&&phone1.value!="号码")
        {
            phone=phone0.value+"-"+phone1.value;            
        }
        if(phone2.value.length>0&&phone2.value!="分机")
        {
            phone=phone0.value+"-"+phone1.value+"-"+phone2.value;
        }
        var mobile=document.getElementById("Mobile").value;
        Mask();
        SetAddress("<br /><br />数据加载中......<br /><br /><img src='../Include/Images/loadimg.gif'>");        
        $j.post("../Handler/AddressHandler.ashx", {Reciver:reciver,Province:Province,City:City,Area:Area,Address:daddress,Post:post,Phone:phone,Mobile:mobile,UserCode:UserCode, OP:"AddBack"},
                function(data){                          
                   window.location.reload();
                }
            );
    }
}
function AddNewAddressBack()
{    
    if(CheckAddress())
    {//debugger;
        var reciver=document.getElementById("T_Name").value;
        var Province=document.getElementById("Province").value;
        var City=document.getElementById("City").value;
        var Area=document.getElementById("Area").value;
        var daddress=document.getElementById("Address").value;
        var post=document.getElementById("Post").value;
        
        var UserCode=document.getElementById("TextBox2").value;
        
        var phone0=document.getElementById("Phone");//区号
        var phone1=document.getElementById("Phone2");//电话号码
        var phone2=document.getElementById("Phone3");//分机号码
    
        var phone="";
        if(phone1.value.length>0&&phone1.value!="号码")
        {
            phone=phone0.value+"-"+phone1.value;            
        }
        if(phone2.value.length>0&&phone2.value!="分机")
        {
            phone=phone0.value+"-"+phone1.value+"-"+phone2.value;
        }
        var mobile=document.getElementById("Mobile").value;
        Mask();
        SetAddress("<br /><br />数据加载中......<br /><br /><img src='../Include/Images/loadimg.gif'>");        
        $j.post("../Handler/AddressHandler.ashx", {Reciver:reciver,Province:Province,City:City,Area:Area,Address:daddress,Post:post,Phone:phone,Mobile:mobile,UserCode:UserCode, OP:"AddBack"},
                function(data){           
                   AddressInit(UserCode);
                   document.getElementById("Province").value=Province;
                }
            );
    }
}
function Next()
{//debugger;
    var reciver=document.getElementById("T_Name");
    if(reciver.value.length<1)
    {
        alert("收货人姓名不能为空");
        reciver.focus();
        return false;
    }
    var Province=document.getElementById("Province");
    if(Province.selectedIndex<1)
    {
        alert("请选择省份");
        Province.focus();
        return false;
    }
    var City=document.getElementById("City");
    if(City.selectedIndex<1)
    {
        alert("请选择城市");
        City.focus();
        return false;
    }
    var Area=document.getElementById("Area");
    if(Area.selectedIndex<1)
    {
        alert("请选择区县");
        Area.focus();
        return false;
    }
    var daddress=document.getElementById("Address");
    if(daddress.value.length<1)
    {
        alert("请填写具体地址");
        daddress.focus();
        return false;
    }
    var post=document.getElementById("Post");
    if(isNaN(post.value))
    {
        alert("邮编格式错误,请检查您输入法的全角/半角状态！");
        post.focus();
        return false;
    }
    var regular=/^\d{6}$/;
    if(!regular.test(post.value))
    {
        alert("邮编不正确");
        post.focus();
        return false;
    }
    var phone=document.getElementById("Phone");//区号
    var phone1=document.getElementById("Phone2");//电话号码
    var phone2=document.getElementById("Phone3");//分机号码
    var phone0="";

    var mobile=document.getElementById("Mobile");
    if((phone1.value.length==0||phone1.value=="号码")&&mobile.value.length==0)
    {alert("手机或固定电话必填一个"); return false;}
    
    if(phone.value.length>0&&phone.value!="区号"||phone1.value.length>0&&phone1.value!="号码"||phone2.value.length>0&&phone2.value!="分机")
        {
        if((phone.value.length>0&&phone.value!="区号")||(phone1.value.length>0&&phone1.value!="号码"))
        {
            if(isNaN(phone.value)||isNaN(phone1.value))
            {
                alert("固定电话格式错误,请检查您输入法的全角/半角状态！");
                return false;
            }
            var reg=/^\d{3,4}$/;//检查区号
            var reg1=/^\d{7,8}$/;//检查电话号码
            if(!reg.test(phone.value)||!reg1.test(phone1.value))
            {
                alert("固定电话格式错误");
                return false;
            }
        }
        if(phone2.value.length>0&&phone2.value!="分机")
        {
            if(isNaN(phone.value)||isNaN(phone1.value)||isNaN(phone2.value))
            {
                alert("固定电话格式错误,请检查您输入法的全角/半角状态！");
                return false;
            }
            var reg=/^\d{3,4}$/;//检查区号
            var reg1=/^\d{7,8}$/;//检查电话号码
            var reg2=/^\d{1,5}$/;//检查分机号
            if(!reg.test(phone.value)||!reg1.test(phone1.value)||!reg2.test(phone2.value))
            {
                alert("固定电话格式错误");
                return false;
            }
        }
        if(phone1.value.length>0&&phone1.value!="号码")
        {
            phone0=phone0.value+"-"+phone1.value;            
        }
        if(phone2.value.length>0&&phone2.value!="分机")
        {
            phone0=phone0.value+"-"+phone1.value+"-"+phone2.value;
        }
    }
    if(mobile.value.length>0)
    {
        if(isNaN(mobile.value))
        {
            alert("手机号码含有非法字符");
            mobile.focus();
            return false;
        }
        var reg=/^\d{11}$/;//检查手机
        if(!reg.test(mobile.value))
        {
            alert("请输入正确的11位数手机号码");
            mobile.focus();
            return false;
        }
        /*
        var myreg = /^(((13[0-9]{1})|159|153)+\d{8})$/;
        var flag=myreg.test(mobile.value);
        if(!flag)
        {
            alert("手机号码格式错误");
            mobile.focus();
            return false;
        }
        */
    }
    var Delivers=document.getElementsByTagName("input");
    var Deliver;
    for(var i=0;i<Delivers.length;i++)
    {
        if(Delivers[i].checked&&Delivers[i].type=="radio")
        {
            Deliver=Delivers[i].value;
        }
    }
    var DeliverTime=document.getElementById("DeliverTime").value;
    var txtMemo=document.getElementById("txtMemo").value;
    $j.post("../Handler/AddressHandler.ashx", {Reciver:reciver.value,Province:Province.value,City:City.value,Area:Area.value,Address:daddress.value,Post:post.value,Phone:phone0,Mobile:mobile.value,Deliver:Deliver,DeliverTime:DeliverTime,txtMemo:txtMemo, OP:"Next"},
                function(data){             
                   if(data)
                   {             
                       window.location.href="OrderPay.aspx";
                   }
                   else
                   {
                        alert("数据写入失败,请重试");
                   }
                }
            );
}
//物流类型选择
function SelectDeliverType(obj)
{    
    var delivers=document.getElementsByName("Deliver");
   
    if(obj.value=="rbtExpress")
    {
        document.getElementById("Deliver").style.display="block";        
    }
    else
    {
        document.getElementById("Deliver").style.display="none";
        for(var j=0;j<delivers.length;j++)
        {
            delivers[j].checked=false;
        }
        CalculateYF(obj);
    }
}
//快递选择
function CalculateYF(obj)
{
//    document.getElementById("txtwlfy").value="0";
   Mask();
    var Zamt=document.getElementById("div_zjj").value;
    var Free=document.getElementById("Hid_Free").value;

        var deliver=obj.value;
        var oCity=document.getElementById("City");
        if(oCity.selectedIndex<1)
        {
            alert("请选择城市!");
            oCity.focus();
            HidMask();
            return;
        }
        var city=oCity.value;
         $j.post("../Handler/AddressHandler.ashx", {deliver:deliver,city:city, OP:"WLINFO"},
                    function(data){ 
                      document.getElementById("Hid_Standard").value=data;                      
                      if(data.length>0)
                      {                    
                          var array=data.split('|');
                          if(array.length!=6)
                          {
                            alert("此物流在所选区域收费标准不明确，请选择其它物流方式");
                            obj.checked=false;
                            HidMask();
                            return;
                          }
                          else
                          {
                            if(obj.value!=array[0])
                            {
                                alert("所选物流与其收费标准不匹配，请重新选择物流公司");
                                obj.checked=false;
                                HidMask();
                                return;
                            }
                            if(oCity.value!=array[1])
                            {
                                alert("所选城市已改变，请重新选择物流方式!");
                                obj.checked=false;
                                HidMask();
                                return;
                            }
                            if(array[2].length<1||array[3].length<1||array[4].length<1||array[5].length<1)
                            {
                                alert("所选物流在所选区域收费标准不齐全，请选择其它物流方式!");
                                obj.checked=false;
                                HidMask();
                                return;
                            }
                            else
                            {
                               var weight=document.getElementById("div_spzzl").value;
                                if(Math.round(parseFloat(weight)*1000)/1000<0.001)
                                {
                                    document.getElementById("txtwlfy").value="0.00";
                                    HidMask();
                                    return;
                                }  
                               if(parseFloat(Free)>0)  //判断是否免运费
                                {
                                    if(parseFloat(Zamt)<parseFloat(Free))  
                                    {
                                        if(parseFloat(weight)>parseFloat(array[2]))
                                        {
                                            var left=Math.round((parseFloat(weight)-parseFloat(array[2]))*1000)/1000;
                                            var res=left/parseFloat(array[4]);
                                            var aa=Math.ceil(res);
                                            var more=parseFloat(array[3])+aa*parseFloat(array[5]);
                                            document.getElementById("txtwlfy").value=more;
                                        }
                                        else
                                        {
                                            document.getElementById("txtwlfy").value=array[3];
                                            
                                        }
                                    }
                                    else
                                    {
                                      document.getElementById("txtwlfy").value="0.0";
                                    }
                                    HidMask();
                                }
                                else
                                {
                                  if(parseFloat(weight)>parseFloat(array[2]))
                                    {
                                        var left=Math.round((parseFloat(weight)-parseFloat(array[2]))*1000)/1000;
                                        var res=left/parseFloat(array[4]);
                                        var aa=Math.ceil(res);
                                        var more=parseFloat(array[3])+aa*parseFloat(array[5]);
                                        document.getElementById("txtwlfy").value=more;
                                    }
                                    else
                                    {
                                        document.getElementById("txtwlfy").value=array[3];
                                        
                                    }
                                    HidMask();
                                } 
                                
                            }
                          }

                      }
                      else
                      {
                        alert("所选物流在此区域没有服务，主选择其它物流方式！");
                        obj.checked=false;  
                        document.getElementById("txtwlfy").value="0";   
                        HidMask();                   
                      }
                    Suan('');
                            
                    }


                );

       
}